/*******************************************
 *                                         *
 *******************************************/

#include "Tetris.h"

//Constructor
Tetris::Tetris (void)
{
	this->CurrentGameState = InMainMenu;
	this->GameMainMenu = NULL;
}

//Destructor
Tetris::~Tetris (void)
{
	if (this->GameMainMenu)
		delete GameMainMenu;
}

//Public Methods
BOOL Tetris::Init (HINSTANCE Instance, LPCWSTR WindowTitle, UINT Width, UINT Height, BOOL FullScreen)
{
	BOOL Success = TRUE;

	if (!(this->GameWindow = new Window(Instance, WindowTitle, Width, Height, FullScreen)))
		Success = FALSE;
	else
	{
		if (!(this->Renderer = new GraphicsDevice(*GameWindow)))
			Success = FALSE;
		else
		{
			if (!(this->Keyboard = new GameKeyboard(GameWindow->GetWindowInstance(), GameWindow->GetWindowHandle())))
				Success = FALSE;
			if (!(this->Mouse = new GameMouse(GameWindow->GetWindowInstance(), GameWindow->GetWindowHandle())))
				Success = FALSE;
			this->GameMainMenu = new MainMenu(this->Renderer->GetDrawDevice(), (float)this->GameWindow->GetScreenWidth(), (float)this->GameWindow->GetScreenHeight());
			this->MainGameBoard = new GameBoard(this->Renderer->GetDrawDevice(), (float)this->GameWindow->GetScreenWidth(), (float)this->GameWindow->GetScreenHeight());
			this->GameTimer.Start();
			
			this->FPS = new Text(this->Renderer->GetDrawDevice(), 20, FALSE, L"Times New Roman");

			this->GameWindow->Show();
		}
	}

	return Success;
}

BOOL Tetris::Update (void)
{
	//Update the Window
	if (this->IsRunning = this->GameWindow->UpdateWindowMsg())
	{
		switch (this->CurrentGameState)
		{
			case InMainMenu:
				{
					this->IsRunning = this->LogicMainMenu();
					this->RenderMainMenu();
				} break;

			case InGame:
				{
					this->IsRunning = this->LogicInGame();
					this->RenderInGame();
				} break;

			default:
				this->IsRunning = FALSE;
		}
	}
	this->FPS->SetText(this->GameTimer.GetFPS_String());
	return this->IsRunning;
}

///////////////////
//Private Methods//
///////////////////

BOOL Tetris::LogicMainMenu (void)
{
	this->IsRunning = this->GameMainMenu->Logic(*this->Keyboard, this->CurrentGameState);
	return this->IsRunning;
}

void Tetris::RenderMainMenu (void)
{
	//Start Scene
	this->Renderer->BeginRendering();
		//Draw Scence
		this->GameMainMenu->Render();
		this->FPS->Draw();
	//End Scene
	this->Renderer->EndRendering();
	this->GameTimer.Update();
}

BOOL Tetris::LogicInGame (void)
{
	this->IsRunning = this->MainGameBoard->Logic(*this->Keyboard, this->CurrentGameState);
	return this->IsRunning;
}

void Tetris::RenderInGame (void)
{
	//Start Scene
	this->Renderer->BeginRendering();
		//Draw Scence
		this->MainGameBoard->Render();
		this->FPS->Draw();
	//End Scene
	this->Renderer->EndRendering();
	this->GameTimer.Update();
}